(function(){
	var defaults = {
		url: '',
		param: {},
		mapping: [],
		pageBarId: ''
	};
	$.fn.extend({ 
		datagrid: function(options) { 
			var $this = $(this);
			
			$.extend(true, defaults, options);

			$this.empty();
			
			if(!$this.hasClass("datagrid")) {
				$this.addClass("datagrid");
			}
			
			$.post(defaults.url, defaults.param, function(data) {
				
				var showCell = [];  // 需要进行展示的列
				var $colgroup = [];  // 列宽
				var $thead = [];  // 表头
				var $tbody = [];  // 表体
				var $table = [];
				
				$colgroup.push('<colgroup>');
				$thead.push('<thead><tr>');
				for(var i = 0; i < defaults.mapping.length; i++) {
					var cell = defaults.mapping[i];  // {text: '编号', field: 'id', width: '25'}
					if(!cell.hide) {
						showCell.push(cell);
						$colgroup.push('<col width="' + (cell.width ? cell.width : '') + '">');
						$thead.push('<th>' + cell.text + '</th>');
					}
				}
				$colgroup.push('</colgroup>');
				$thead.push('</tr></thead>');
				
				if(data) {
					$tbody.push('<tbody>');
					for(var i = 0; i < data.list.length; i++) {
						var model = data.list[i];
						
						var tr = [];
						tr.push('<tr>');
						for(var j = 0; j < showCell.length; j++) {
							var val = model[showCell[j].field];
							if(!val) {
								val = showCell[j].embel();
							}
							tr.push('<td>' + val + '</td>');
						}
						tr.push('</tr>');
						
						$tbody.push(tr.join(""));
					}
					$tbody.push('</tbody>');
					
				}
				
				$table.push('<div class="table-wrap"><table>');
				$table.push($colgroup.join(""));
				$table.push($thead.join(""));
				$table.push($tbody.join(""));
				$table.push('</table></div>');
				
				$this.append($table.join(""));
				
				$(".datagrid tbody tr").hover(function (){  
					$(this).addClass("hover");
			    },function (){  
			    	$(this).removeClass("hover");
			    }); 
				
				
				var $pageBar = [];
				$pageBar.push('<div class="page-wrap">');
				if(data) {
					var currentPage = data.pageNumber;
					var totalPage = data.totalPage;
					
					if(totalPage <= 0 || currentPage > totalPage) {
						// wtf??
					} else {
						var startPage = currentPage - 4;
						if (startPage < 1) {
							startPage = 1;
						}
						
						var endPage = currentPage + 4;
						if (endPage > totalPage) {
							 endPage = totalPage;
						}
						
						if (currentPage <= 8) {
							startPage = 1;
						}
						if ((totalPage - currentPage) < 8) {
							endPage = totalPage;
						}
						
						if (currentPage == 1) {
							$pageBar.push('<a href="javascript:;">&lt;</a>');
						} else {
							$pageBar.push('<a href="#(link)#(currentPage - 1)#(append)" class="enable">&lt;</a>');
						}
						
						if (currentPage > 8) {
							$pageBar.push('<a href="#(link)#(1)#(append)" class="enable">1</a>');
							$pageBar.push('<a href="#(link)#(2)#(append)" class="enable">2</a>');
							$pageBar.push('<a href="javascript:;">...</a>');
						}
						
						for(var i = startPage; i <= endPage; i++) {
							if (currentPage == i) {
								$pageBar.push('<a href="javascript:;" class="current">' + i + '</a>');
							} else {
								$pageBar.push('<a href="#(link)#(2)#(append)" class="enable">' + i + '</a>');
							}
						}
						
						if ((totalPage - currentPage) >= 8) {
							$pageBar.push('<a href="javascript:;">...</a>');
							$pageBar.push('<a href="#(link)#(totalPage - 1)#(append)" class="enable">' + (totalPage - 1) + '</a>');
							$pageBar.push('<a href="#(link)#(totalPage)#(append)" class="enable">' + totalPage + '</a>');
						}
						
						if (currentPage == totalPage) {
							$pageBar.push('<a href="javascript:;">&gt;</a>');
						} else {
							$pageBar.push('<a href="#(link)#(currentPage + 1)#(append)" class="enable">&gt;</a>');
						}
						
						$pageBar.push('<span>到第<input class="page-input">页</span><span class="page-button">确定</span><span>共&nbsp;' + data.list.length + '&nbsp;条</span>');
						$pageBar.push('<span id="' + defaults.pageBarId + '"></span>');
					}
				}
				$pageBar.push('</div>');
				
				$this.append($pageBar.join(""));
				
				$('#' + defaults.pageBarId).select({
					width: 80,
					margin: '0 0 0 10px',
					items: [
						{value: 20, text: '20 条/页'},
						{value: 30, text: '30 条/页'},
						{value: 50, text: '50 条/页'}
					]
				});
			});
        }
	});
})(jQuery);